package com.devicemantra.coordinate.jaxb;

import java.io.StringWriter;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.devicemantra.coordinate.exceptions.jaxb.CJAXBContextCreationException;
import com.devicemantra.coordinate.exceptions.jaxb.CJAXBCreateMarshallerException;
import com.devicemantra.coordinate.exceptions.jaxb.CJAXBException;
import com.devicemantra.coordinate.exceptions.jaxb.CJAXBMarshalException;

/** This class reads from the ServletInputStream , unmarshalls the XML request using JAXB 
 *  and returns a new Request Object.
 * @author amith
 *
 */

public class JAXBResponseFactory {
	
	public static String createResponse(JAXBElement<?> pResponse) throws CJAXBException
	{
		Marshaller lMarshaller = null;
		try {
			lMarshaller = JAXBContextFactory.createMarshaller();
		} catch (CJAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ObjectFactory lFactory = new ObjectFactory();
		StringWriter lStringWriter = new StringWriter();
		JAXBElement<?> lJAXBResponse = (JAXBElement<?>) pResponse;
		try {
			lMarshaller.marshal(lJAXBResponse, lStringWriter);
		} catch (JAXBException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			throw new CJAXBException("JAXB ERROR:Could not Marshal Response object");
		}
		return lStringWriter.toString();
	}
	
	
	
	
	public static String createResponse(Response pResponse) throws CJAXBException
	{
		Marshaller lMarshaller = null;
		if(lMarshaller == null )
			System.out.println("[BEFORE] lMarshaller is  null ");
		else
			System.out.println("[BEFORE] lMarshaller is not  null ");
		lMarshaller = JAXBContextFactory.createMarshaller();
		if(lMarshaller == null )
			System.out.println("[AFTER] lMarshaller is  null ");
		else
		System.out.println("[AFTER] lMarshaller is not  null ");
			
		ObjectFactory lFactory = new ObjectFactory();
		StringWriter lStringWriter = new StringWriter();
		JAXBElement<Response> lJAXBResponse = lFactory.createCResponse(pResponse);
		try {
			if(lMarshaller == null )
				System.out.println("lMarshaller is  null ");
			else
				System.out.println("lMarshaller is not  null ");
			
			lMarshaller.marshal(lJAXBResponse, lStringWriter);
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new CJAXBException("ERROR:Could not Marshal Response object");
		}
		
		return lStringWriter.toString();
	}
	
	public static String createStatusResponse(int pRespCode, String pResponseStr) throws CJAXBException
	{
		ObjectFactory lFactory = new ObjectFactory();
		Response lResponse = lFactory.createResponse();
		ResponseData lRespData = lFactory.createResponseData();
		Status lStatus = new Status();
		lStatus.setCRespCode(pRespCode);
		lStatus.setCReason(pResponseStr);
		lRespData.setCStatus(lStatus);
		lResponse.setCResponseData(lRespData);
		String lResponseXmlStr = createResponse(lResponse);
		return lResponseXmlStr;
	}
}
